import * as vscode from 'vscode';
import { cosmiconfigSync } from 'cosmiconfig';
import { DictArray } from './typing';

export const DICT_ARRAY_FILE_NAME = 'projectDictionary';
const explorerSync = cosmiconfigSync(DICT_ARRAY_FILE_NAME, {
  stopDir: vscode.workspace.rootPath,
});

export const defaultConfigInfo: IConfigInfo = {
  config: {
    dict: [],
  },
  filePath: '',
};

export const configInfo: IConfigInfo = JSON.parse(JSON.stringify(defaultConfigInfo));

export function updateConfig() {
  explorerSync.clearSearchCache();
  const { config, filepath: filePath = '' } = explorerSync.search(vscode.workspace.rootPath) || {};

  configInfo.config = config;
  configInfo.filePath = filePath;
}

export interface IConfig {
  dict: DictArray,
}

export interface IConfigInfo {
  config: IConfig,
  filePath: string,
}